# -*- coding: utf-8 -*-

import Core

def get_full_path(target):

    if isinstance(target, Core.AstNodeBase):
        p = target.parent
        path = [target.id]
    else:
        p = target
        while isinstance(p, Core.ReferenceType):
            p = p.type

        if isinstance(p, Core.PrimitiveType):
            return [str(target)]

        p = p.parent
        path = [str(target)]

    while p != None and isinstance(p, Core.AstNodeBase):
        path.append(p.id if isinstance(p, Core.AstNodeBase) else p.sig)
        p = p.parent

    path.reverse()
    return path


class VisitorUtil:
    def __init__(self, indent=' '*4):
        self.indent = indent
        self.depth = 0

    def echo(self, msg):
        print self.indent * self.depth + msg

    def acceptMembers(self, obj):
        self.depth += 1
        for m in obj.elements:
            m.accept(self)
        self.depth -= 1

    def getAbsPath(self, obj, sep='.'):
        return sep.join(get_full_path(obj))

    def getScope(self, obj):
        path = get_full_path(obj)
        if not(path == None or len(path) == 0):
            path.pop()
        else:
            path = []
        return path


# Load helper functions.
from Helper import *
